$(function() {
    if (typeof(Storage) !== "undefined")
    {
        $('.wd-loading').show();

        $.post(localStorage.domain_app+"/webservice/toDo/index.php", {
            page: 1,
            user_id: localStorage.userID
        }, function(res) {
            $('.wd-list-todo').html(res.html);
            $('.wd-loading').hide();
            saveToDoID();
            deleteToDo();
            pagingToDoByDay();
            up_down();
            checkToDoComplete();
        }, "json");
    }
});
function checkToDoComplete() {
    var check_complete = false;
    $('.wd-checkbox').click(function() {
        
        if (check_complete === false) {
            check_complete = true;
            var complete = 0;
            if ($(this).is(":checked")) {
                complete = 1;
            }
            $.post(localStorage.domain_app+"/webservice/toDo/checkToDoComplete.php", {
                todo_id: $(this).attr('rel'),
                user_id: localStorage.userID,
                complete: complete
            }, function(res) {
                print_alert(res.msg);
                check_complete = false;
            }, "json");
        }
    });
}
function up_down() {
    $(".act_up_down").click(function() {

        if ($(this).children(".wd-down").hasClass("down")) {
            $(this).children(".wd-down").removeClass("down");
            $(this).children(".wd-down").addClass("up");
            $(this).children(".wd-down").html('<img src="img/up.png"/>');
            $("." + $(this).children(".wd-down").attr("rel")).show();
        } else {
            $(this).children(".wd-down").removeClass("up");
            $(this).children(".wd-down").addClass("down");
            $(this).children(".wd-down").html('<img src="img/down.png"/>');
            $("." + $(this).children(".wd-down").attr("rel")).hide();
        }
    });
}
function pagingToDoByDay() {
    var paging = false;
    $('.showmore').click(function() {
        $('.wd-checkbox').unbind();
        $('.wd-loading').show();
        var show_more = $(this);
        var str_day = show_more.attr("rel");
        var end_paging = 0;
        var page = 0;
        if (str_day == "past") {
            page = $('#page_past').val();
            end_paging = $('#end_paging_past').val();
        } else if (str_day == "today") {
            page = $('#page_today').val();
            end_paging = $('#end_paging_today').val();
        } else if (str_day == "tomorrow") {
            page = $('#page_tomorrow').val();
            end_paging = $('#end_paging_tomorrow').val();
        } else if (str_day == "later") {
            page = $('#page_later').val();
            end_paging = $('#end_paging_later').val();
        }
        if (end_paging == 0) {

            if (paging == false) {
                paging = true;
                $.post(localStorage.domain_app+"/webservice/toDo/getToDoByDay.php", {
                    page: page,
                    user_id: localStorage.userID,
                    str_day: str_day
                }, function(res) {
                    
                    show_more.before(res.html);
                    if (res.end_paging == 1) {
                        show_more.hide();
                    }
                    if (str_day == "past") {
                        $('#page_past').val(res.page);
                        $('#end_paging_past').val(res.end_paging);
                    } else if (str_day == "today") {
                        $('#page_today').val(res.page);
                        $('#end_paging_today').val(res.end_paging);
                    } else if (str_day == "tomorrow") {
                        $('#page_tomorrow').val(res.page);
                        $('#end_paging_tomorrow').val(res.end_paging);
                    } else if (str_day == "later") {
                        $('#page_later').val(res.page);
                        $('#end_paging_later').val(res.end_paging);
                    }
                    saveToDoID();
                    deleteToDo();
                    
                    checkToDoComplete();
                    $('.wd-loading').hide();
                    paging = false;
                }, "json");
            } else {
                return false;
            }

        }
    });
}